var ModuleUtil = {
  setTableColData: function (tableColConfig) {
    return Promise.all([
      this.setMaterialCol(tableColConfig),
      this.setOrderInfoCol(tableColConfig),
      this.setWorkshopCol(tableColConfig)
    ]).then(function (resArray) {
      return resArray;
    });
  },

  setMaterialCol: function (tableColConfig) {
    return Promise.all([
      this.getMaterialTypeList(),
      this.getShowMaterialTypeList()
    ]).then(function (resArray) {
      var allTypeList = resArray[0];
      var showTypeList = resArray[1];
      tableColConfig.setMaterialTypeConfig(allTypeList, showTypeList);
      return tableColConfig;
    });
  },

  getMaterialTypeList: function () {
    return ajaxRequest('companyMaterialType/getCompanyMaterialTypeListByPage', {
      status: 1
    }, false).then(function (res) {
      materialTypeList = res.returnData;
      materialTypeList = _.map(materialTypeList, 'materialType');
      materialTypeList = _.uniq(materialTypeList);
      return materialTypeList;
    });
  },

  getShowMaterialTypeList: function () {
    return ajaxRequest('displayView/getMaterialIsShow').then(function (data) {
      if (SysUtil.isEmpty(data)) {
        data = [];
      } else {
        data = JSON.parse(data.data);
      }
      return data;
    });
  },

  setOrderInfoCol: function (tableColConfig) {
    ajaxRequest('tableCoucmnlConfig/getConfigByName', {
      tableName: 'materialMatchBoard'
    }).then(function (config) {
      var tableHideColumns;
      if (config === null) {
        tableHideColumns = [];
      } else {
        var hideColumn = config.hideColumn;
        if (SysUtil.isEmpty(hideColumn)) {
          tableHideColumns = [];
        } else {
          var obj = JSON.parse(hideColumn);
          tableHideColumns = obj.columns;
        }
      }

      tableColConfig.setOrderInfoConfig(tableHideColumns);
      return tableColConfig;
    });
  },

  setWorkshopCol: function (tableColConfig) {
    return ajaxRequest('basicData/queryCycle', {
      view: 1
    }, false).then(function (res) {
      var workshopList = res.returnData;
      if (SysUtil.isEmpty(workshopList)) {
        workshopList = [];
      }

      tableColConfig.setWorkshopConfig(workshopList);
      return tableColConfig;
    });
  }
};
